***************************************************
/* This do-file replicates Figure 4 of the paper */
***************************************************

set more off 

cd "" /*input directory here with cleaned data south_africa_GHS using the ad hoc GHS do-file */

use south_africa_GHS.dta, clear 

bys household_id year: egen grant_h=max(child_grant)  /* variable at the household level equal to 1 if anybody in the household receives the grant */ 


gen cohort= year-age /* No Information for cohort, given that GHS takes place in July, year - age gives a cohort born from july of t-1 to june of t. See paper for detailed explanation */  

drop if  race==3 | race==4 /* Exclude population groups that do not receive CSG to a significant extent, see text for explanation */ 



*********************************************
* CSG take- up by youngest cohort and year ** 
*********************************************

* Merge Child-Level information on grant with recipient mothers * 

preserve 

keep household_id cohort mother_id child_grant age year

rename mother_id id 
rename cohort cohort_child 
rename child_grant grant_child
rename age age_child 

sort household_id id  

save children_GHS.dta, replace

restore 

merge 1:m household_id year id using children_GHS.dta 

erase children_GHS.dta

keep if _merge==3 /* this only keeps mother with children observed in the household */ 

keep if sex==2 /* Excludes a few mistmatched observations (matched with man, <1%) */ 
drop if age<age_child+15 /* Excludes if age of the child is too young wrt to mother (1%) */ 


* Keep only the youngest child for each mother * 

egen id_mother=group(household_id id year)

drop child_grant 

bys id_mother: egen youngest_child_cohort=max(cohort_child) /* for each individual mother, identify the youngest child */ 

keep if cohort_child==youngest_child_cohort /* keeps only observations for the youngest.  */ 

bys id_mother: egen child_grant_mother=max(grant_child) /* This defines grant receipt at the mother level. Nothing changes if for the mother has only one 
youngest child. If the mother has two youngest children of the same cohort (roughly 3% of the sample), it creates a variable equal to 1 if any of the two children receives the child support grant  */


keep if cohort_child>=1991 & cohort_child<=1995 /* Relevant cohorts */

duplicates drop id_mother, force /*keep only one observation per mother */ 


***********************************************
** First Stage Estimation, by Marital Status ** 
***********************************************

drop if year==2002 /*exclude 2002 because in this year CSG information is only coded at the household level */ 

keep if cohort_child>=1992 & cohort_child<=1993 /* Relevant cohorts */

gen CSG=(cohort_child==1993) 

gen coef_un=.
gen ub_un=. 
gen lb_un=. 

gen coef_m=. 
gen ub_m=. 
gen lb_m=. 


forvalues i= 2003 (1) 2011 {

reg child_grant_mother 1.CSG if married==0 & year==`i' [w=weight], cluster(household_id)
replace coef_un=_b[1.CSG] if year==`i'
replace ub_un=_b[1.CSG] + _se[1.CSG]*1.96 if year==`i'
replace lb_un=_b[1.CSG] - _se[1.CSG]*1.96 if year==`i'

reg child_grant_mother 1.CSG if married==1 & year==`i' [w=weight], cluster(household_id) 

replace coef_m=_b[1.CSG] if year==`i'
replace ub_m=_b[1.CSG] + _se[1.CSG]*1.96 if year==`i'
replace lb_m=_b[1.CSG] - _se[1.CSG]*1.96 if year==`i'

}

egen tag=tag(year married)

************
** Fig 4a **
************

twoway (scatter coef_m year if tag==1, mcolor(gs10)) || (rcap ub_m lb_m year, lcolor(gs10)) ///
||,  graphregion( color(white) ) plotregion(  fcolor(white) ) xlabel(2003 (1) 2011) ///
||, legend(off) xtitle("") ylabel( -0.1 (0.1) 0.4)


************
** Fig 4b **
************

twoway (scatter coef_un year if tag==1, mcolor(black)) || (rcap ub_un lb_un year, lcolor(black)) ///
||,  graphregion( color(white) ) plotregion(  fcolor(white) ) xlabel(2003 (1) 2011) ///
||, legend(off) xtitle("") ylabel( -0.1 (0.1) 0.4)
